<?php
/**
 * SettingController 
 * 
 */
class SettingController extends Zend_Controller_Action 
{
    protected $model;
    protected $room_zone_model;
    protected $mote_gateway;
    protected $status_model;


    protected function _getModel()
    {
      if (null === $this->model) {
          require_once APPLICATION_PATH . '/models/Setting.php';
          $this->model = new Model_Setting();
      }
      return $this->model; 
    }
    
    protected function _getRoomZoneModel()
    {
      if (null === $this->room_zone_model) {
          require_once APPLICATION_PATH . '/models/RoomZone.php';
          $this->room_zone_model = new Model_RoomZone();
      }
      return $this->room_zone_model;
    }     

    protected function _getStatusModel()
    {
      if (null === $this->status_model) {
        require_once APPLICATION_PATH . '/models/Status.php';
        $this->status_model = new Model_Status();
      }
      return $this->status_model;
    }

    protected function _getMoteGatewayModel()
    {
      if (null === $this->mote_gateway) {
        require_once APPLICATION_PATH . '/models/MoteGateway.php';
        $this->mote_gateway = new Model_MoteGateway();
      }
      return $this->mote_gateway;
    }


    /**
     *
     */
    public function indexAction() 
    {
      // http://localhost/ac2/setting
      $this->_helper->layout->disableLayout(); // disable layouts
      
      $model  = $this->_getModel();
      $mode   = ($model->isHeat() == true) ?  "heat" : "cool";
      $scale  = ($model->isCelsius() == true) ?  "C" : "F";
      
      $xml  = "<settings>";
      $xml .= "<mode value=\"$mode\" />";
      $xml .= "<scale value=\"$scale\" />";                    
      $xml .= "</settings>";
      
      header('Content-type: text/xml'); 
      echo $xml;
      exit;
    }

    /* 
     * User changed mode from Index page
     *
     */
    public function modeAction() 
    {
      $request = $this->getRequest();
      $mode    = $request->getParam("value");
      // update SystemController
      $this->_getModel()->setMode($mode); 
      // udpate Motes
      $mgate = $this->_getMoteGatewayModel();
      $mmode  = ($mode == "heat") ? Model_MoteGateway::MODE_HEAT : Model_MoteGateway::MODE_COOL;
      $rzs = $this->_getRoomZoneModel()->fetchEntries();
      foreach ($rzs as $rz) {
        $mgate->setMode($rz['zone_id'], $mmode);
      }
      //exit;
      return $this->_helper->redirector->gotoSimple('index',$request->getParam("return"));
    }
    
    /* 
     * User changed scale from Index page
     *
     */
    public function scaleAction() 
    {
      $request = $this->getRequest();
      $this->_getModel()->setScale($request->getParam("value"));
      return $this->_helper->redirector->gotoSimple('index',$request->getParam("return"));
    }
    
    
    public function statusAction() 
    {
      $request = $this->getRequest();
      $data['zone_id']    = $request->getParam("zone");
      $data['vent_state'] = $request->getParam("state");
      $data['mode']       = $request->getParam("mode");
      
      $data['zone_id'] = 16 * round($data['zone_id']/16);
      
      $this->_getStatusModel()->save($data);
      exit;
    }

}
